var app = getApp();
var util = require('../../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isaufocus:true,
    shopid: -1,
    inputFBNum:"",
    rightDatas: [], 
    isInput: false,  //添加商品数量弹框及输入框焦点
    isInput2: false,  //特殊需求备注弹框的输入框焦点
    inputNum: "",   //添加商品弹窗的输入框中数量
    inputIndex: -1,    //主产品索引值 
    shangpin: '',
    isfeibiao: false,  //添加非标品弹窗的显隐
    fbName: '',  //非标品名称
    fbNum: 0,   //非标品数量
    firstPerson: '',  //非标品的单位
    tanimg: "",   //弹窗商品图片
    tanname: "",  //弹窗商品名称
    tanunit: "",  //弹窗商品单位
    tangui: "",   //弹窗规格名称
    tannum: "",   //弹窗商品数量
    hide_good_box: true,  //动画小球的显隐
    tszhinput: "",     //备注输入框内容
    tszhcontent: "",   //备注内容
    page: 1,     //当前页码
    allpage:1,
    load: "搜索中...",
    selectdanwei: false, //选择单位弹窗
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    content: '',  //键盘初始空字符串
    loading:1,
    assign_super_index:-1,  //选择的供应商
    tan_is_auxiliary:0,  //当前商品是否开启辅助单位  0关闭，1开启
    aux_kucun_index:-1,   //库存商品辅助单位索引
    tan_shop_ku:0,  //当前商品是否属于库商品
    group_name:'',   //所加入的集团名称
    assign_superlist:[],
    aux_kucun_list:[],
    aux_pf_list:[], //gy_pf辅助单位列表
    aux_pf_index:-1, //gy_pf辅助单位索引
    is_move_gif:false,  //按钮动图
    shop_submit_price:0  //提交价
  },
  // 打开特殊需求备注弹窗
  opentszh: function () {
    this.setData({
      isInput2: true,
      istszh: true,
      tszhinput: this.data.tszhcontent,
    })
  },
  // 特殊需求备注弹窗输入内容
  tszhinput: function (e) {
    this.setData({
      tszhinput: e.detail.value
    })
  },
  // 清除备注内容
  cleartszh: function () {
    this.setData({
      tszhinput: ''
    })
  },
  // 关闭特殊需求备注弹窗
  tszhcancel: function () {
    this.setData({
      isInput2: false,
      istszh: false
    })
  },
  // 确定并关闭需求备注弹窗
  tszhconfirm: function () {
    this.setData({
      isInput2: false,
      istszh: false,
      tszhcontent: this.data.tszhinput
    })
    console.log(this.data.tszhcontent);
  },
  // 清空输入内容
  delcont:function(){
    var that = this;
    setTimeout(function (){
      that.setData({
        isaufocus:true,
        shangpin:'',
        rightDatas:[],
        guige: [],
        page: 1,
        load: "搜索中"
      })
    },100)
  },
  close_focus:function(){
    this.setData({
      isaufocus:false
    });
  },
  //点击切换
  mySelect(e) {
    util.hideBuyModal(this,"selectdanwei");
    this.setData({
      firstPerson: e.target.dataset.me,
      value: e.target.dataset.value
    })
  }, 
  //供应商未报价
  zero_price_but:function(){
    wx.showLoading({
      title:'供应商未报价',
      duration:500
    })
  },
  // 打开规格的收藏内容
  spesc_but: function (e) {
    var index = e.currentTarget.id;
    var is_up=e.currentTarget.dataset.is_up;  //是否下架及供应商数量
    var is_ku=e.currentTarget.dataset.is_ku;  //当前商品是否属于库存商品
    var is_zi_cai=this.data.is_zi_cai;  //是否自采
    var specs_count=e.currentTarget.dataset.specs_count;  //是否多规格
    var tan_is_auxiliary=e.currentTarget.dataset.is_auxiliary;  //是否开启辅助单位
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){
      if(is_up*1+is_zi_cai*1==0 && is_ku==0){
        return;
      };
      if(is_ku==1){   //库存商品
        this.setData({
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_shop_ku:1,
          aux_kucun_index:-1,
          aux_kucun_list:[],
          tan_is_auxiliary:tan_is_auxiliary
        });
        if(tan_is_auxiliary==1){
          this.aux_kucun_fun(e.currentTarget.dataset.shopid);
        };
      }else if(is_ku==0){  //非库存商品
        this.setData({
          tan_shop_ku:0,
          tan_is_auxiliary:tan_is_auxiliary,
          assign_superlist:[]
        });
        console.log(specs_count);
        console.log(is_zi_cai);
        if(specs_count==0 && is_zi_cai==0){  //仅有主规格+非自采
          this.setData({
            is_move_gif:true
          });
          this.assign_super_fun(e.currentTarget.dataset.shopid,tan_is_auxiliary);
        }else if(is_zi_cai==1){  //自采
          this.setData({
            is_move_gif:false
          });
        };
      };
    }else{
      if(this.data.rightDatas[index].price_type<=2){  //供应-批发
        this.setData({
          aux_pf_index:-1,
          aux_pf_list:[],
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_is_auxiliary:tan_is_auxiliary,
        })
        if(tan_is_auxiliary==1 && specs_count==0){  //仅有主规格
          this.aux_gy_pf_fun(e.currentTarget.dataset.shopid);
        }; 
      }else{  //其他模式的企业
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          is_move_gif:false
        });
      }
    };
    this.setData({
      shopid: e.currentTarget.dataset.shopid,
      show_index:index,
      shop_is_same:this.data.rightDatas[index].is_same,
      ischoose_index: -1,
      tszhcontent: ''
    });
    this.tancontent(); 
  },
  // 收藏弹窗内容展示
  tancontent: function () {
    var that = this;
    app.AppHttp('api/Order/OrderGoodAlert', {
      id: that.data.shopid,
      order_id: that.data.order_id
    }, function (res) {
      console.log(JSON.stringify(res.data));
      console.log(res.data.data.length);
      that.setData({
        taninfo: res.data.data,    //弹窗数据   
        isinput_col: true,
        isaufocus:false,
        shopnum:'',
        shoppr_num:'',
        assign_super_index:-1
      });
      if (res.data.data.length == 1) {
        that.setData({
          ischoose_index: 0,  //当规格只有一个时为选中状态
          tszhcontent: res.data.data[0].remark?res.data.data[0].remark:'', 
          shoppr_num: res.data.data[0].pr_num,  //选中的产品是否已添加
          shop_price_type:res.data.data[0].price_type,  //选中的产品价格类型
          shop_price:res.data.data[0].true_price, //选中的产品的价格
          shop_b_price:res.data.data[0].single_price,  //选中的产品B单价格
          shop_sale_price:res.data.data[0].sale_price,  //选中的产品销售价
          shop_level_price:res.data.data[0].level_true_price,  //级别A价
          shop_level_b_price:res.data.data[0].level_single_price,  //级别B价
          tan_ku_price:that.data.shop_is_same==0?res.data.data[0].stock_sale_price:res.data.data[0].stock_price_show,
          tan_ku_num:res.data.data[0].stock_num_show,
          is_tan_ku_price:res.data.data[0].is_show_stock_price,
          is_tan_ku_num:res.data.data[0].is_show_stock_num
        });
      }else{
        that.setData({
          assign_superlist:[]
        });
      }
    });
  },
  // 选定条件后打开底部弹窗
  open_tan_info:function(e){
    if(this.data.ischoose_index>=0){
      util.showBuyModal(this);
      var taninfo = this.data.taninfo;
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.tan_shop_ku==1){    //库存商品
          if(this.data.tan_is_auxiliary==0){   //未开启辅助单位
            this.setData({
              assign_super_index:-1,
              aux_kucun_index:-1,
              aux_kucun_list:[],
            });
          }else if(this.data.tan_is_auxiliary==1){  //开启了辅助单位
            if(this.data.aux_kucun_list.length==1){  //仅有一条辅助单位
              this.setData({
                assign_super_index:-1,
                aux_kucun_index:0,
                shoppr_num: this.data.aux_kucun_list[0].pr_num,
              });
            }else{
              this.setData({
                assign_super_index:-1,
                aux_kucun_index:e.currentTarget.dataset.index,
                shoppr_num: this.data.aux_kucun_list[e.currentTarget.dataset.index].pr_num
              });
            };     
          };
        }else if(this.data.tan_shop_ku==0){  //非库存商品
          if(this.data.assign_superlist.length==0){  //自采
            this.setData({
              aux_kucun_index:-1,
              aux_kucun_list:[],
              assign_super_index:-10010
            });
          }else if(this.data.assign_superlist.length==1 && this.data.is_zi_cai==0){  //仅一个供应商+未开自采
            this.setData({
              aux_kucun_index:-1,
              aux_kucun_list:[],
              assign_super_index:0,
              shoppr_num:this.data.assign_superlist[0].pr_num
            });
          }else{
            if(e.currentTarget.dataset.index!=-10010){
              this.setData({
                shoppr_num:this.data.assign_superlist[e.currentTarget.dataset.index].pr_num
              });
            };
            this.setData({
              aux_kucun_index:-1,
              aux_kucun_list:[],
              assign_super_index:e.currentTarget.dataset.index
            });  
          }
        }
      }else{  //其他
        if(taninfo[this.data.ischoose_index].price_type<=2){  //供应-批发
          if(this.data.tan_is_auxiliary==0){
            this.setData({
              aux_pf_index:-1,
              aux_pf_list:[],
            });
          }else if(this.data.tan_is_auxiliary==1){
            if(this.data.aux_pf_list.length==1){   //仅一条辅助单位
              this.setData({
                aux_pf_index:0,
                shoppr_num:this.data.aux_pf_list[0].pr_num 
              })
            }else{
              this.setData({
                aux_pf_index:e.currentTarget.dataset.index,
                shoppr_num:this.data.aux_pf_list[e.currentTarget.dataset.index].pr_num 
              })
            }
          }
        }   
      }
      let tan_true_price='';
      let tan_single_price='';
      if(this.data.price_type==2){
        tan_true_price=taninfo[this.data.ischoose_index].level_true_price;
        tan_single_price=taninfo[this.data.ischoose_index].level_single_price;
      }else{
        tan_true_price=taninfo[this.data.ischoose_index].true_price;
        tan_single_price=taninfo[this.data.ischoose_index].single_price;
      };
      this.setData({
        content:'',
        isInput: true,
        detail_id:taninfo[this.data.ischoose_index].pr_num >0?1:0,
        tanimg: taninfo[this.data.ischoose_index].cover,
        tanname: taninfo[this.data.ischoose_index].trade_name,
        tanalias:taninfo[this.data.ischoose_index].trade_alias,
        tanspecs:taninfo[this.data.ischoose_index].specs,
        tan_fixed_show:taninfo[this.data.ischoose_index].is_show_price,  //是否显示固定价
        tanunit_index:taninfo[this.data.ischoose_index].help_unit>0?taninfo[this.data.ischoose_index].help_unit:taninfo[this.data.ischoose_index].unit,
        tanunit:taninfo[this.data.ischoose_index].help_unit>0?taninfo[this.data.ischoose_index].help_unit_name:taninfo[this.data.ischoose_index].unit_name,
        tanunit_init:taninfo[this.data.ischoose_index].unit_name,
        tan_true_price:tan_true_price,
        tan_single_price:tan_single_price, 
        tan_sale_price:taninfo[this.data.ischoose_index].sale_price,
        tan_price_type:taninfo[this.data.ischoose_index].price_type
      });
    }else{
      wx.showLoading({
        title:'请选择规格',
        duration:500
      })
    };
  },
  col_setValue:function(){
    this.setData({
      shopnum: this.data.content
    });
  },
  // 取消关闭添加商品弹窗
  cancel_col: function () {
    this.setData({
      isinput_col: false
    });
  },
  // 选择商品规格
  chose_specs: function (e) {
    var index = e.currentTarget.dataset.index;
    var item=this.data.taninfo[index];
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==0 && item.tan_is_auxiliary==1 && index>0){
      wx.showModal({
        title: '提示',
        content:'当非库存商品开启辅助单位功能后，商品其他规格则被锁定',
        showCancel: false,
        confirmText:"我知道了",
        success: function (res) {}
      });   
      return;
    }else if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==0 && (item.is_auxiliary==1 && item.is_aux_up*1+this.data.is_zi_cai*1==0 || item.is_auxiliary==0 && item.is_up*1+this.data.is_zi_cai*1==0)){
      wx.showModal({
        title: '提示',
        content:'当前规格无可选供应商',
        showCancel: false,
        confirmText:"我知道了",
        success: function (res) {}
      });
      return;
    };
    var is_zi_cai=this.data.is_zi_cai;
    console.log(item);
    this.setData({
      ischoose_index: index,
      tszhcontent: item.detail_remark?item.detail_remark:"",
      shopid: item.id,  //选中的产品id
      detail_id:item.pr_num >0?1:0,
      shoppr_num:item.pr_num,  //选中的产品是否已添加
      shop_price_type:item.price_type,  //选中的产品价格类型
      shop_price:item.true_price, //选中的产品的价格
      shop_b_price:item.single_price,  //选中的产品B单价格
      shop_sale_price:item.sale_price,  //选中的产品销售价
      shop_level_price:item.level_true_price,  //级别A价
      shop_level_b_price:item.level_single_price,  //级别B价
      tan_is_auxiliary:item.is_auxiliary,  //当前规格辅助单位是否开启
      shop_is_same:item.is_same,  //是否开启采销同价
      tan_ku_price:item.is_same==0?item.stock_sale_price:item.stock_price_show,
      tan_ku_num:item.stock_num_show,
      is_tan_ku_price:item.is_show_stock_price,
      is_tan_ku_num:item.is_show_stock_num,
      assign_super_index:-1,
      assign_superlist:[],
      aux_pf_index:-1,
      aux_pf_list:[],
    });
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==0){   //模式5+非库存商品 
      this.setData({
        is_move_gif:true
      });
      this.assign_super_fun(item.id,this.data.tan_is_auxiliary); 
    }else{   //其他
      this.setData({
        is_move_gif:this.data.tan_is_auxiliary==1?true:false
      });
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==1 && this.data.tan_is_auxiliary==1){  //库存+辅助
        this.aux_kucun_fun(item.id); 
      }else{
        if(item.price_type<=2){  //供应-批发
          if(this.data.tan_is_auxiliary==1){ //是辅助单位
            this.setData({
              is_move_gif:true
            });
            this.aux_gy_pf_fun(item.id)
          }else if(this.data.tan_is_auxiliary==0){  //非辅助单位
            this.setData({
              is_move_gif:false
            });
            this.open_tan_info();
          } 
        }else{
          this.open_tan_info();
        }
      };
    } 
  },
  // 添加到订单接口
  addorder(e) {
    let that = this;
    var shopnum = this.data.shopnum;
    if (this.data.ischoose_index == -1) {
      wx.showLoading({
        title: '请选择规格',
        duration: 500
      });
      return;
    }else if(that.data.shopnum==0){
      if(that.data.shopnum != ''){ //输入0
        shopnum=0;
      }else if(that.data.shopnum == ''){ //不输入
        shopnum = that.data.shoppr_num;
      };
      if(shopnum==0){
        wx.showLoading({
          title:'输入不能为0',
          duration:500
        });
        return;
      };
    };
    if (shopnum > 9999.99) {
      wx.showToast({
        title: '数量过大'
      })
      return;
    };
    this.setData({
      detail_id:this.data.shoppr_num>0?1:0
    });
    var aux_obj={'is_aux':0,'pr_unit':that.data.tanunit_index,'sub_unit_num':1};
    if(this.data.shop_price_type==1 || this.data.shop_price_type==2){
      if(this.data.price_type==2){ //客户级别价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:this.data.shop_level_price,
            single_price:this.data.shop_level_b_price
          });
        }
      }else if(this.data.price_type==1){ //客户历史价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:0,
            single_price:0
          });
        };
      }else if(that.data.price_type==4){  //客户固定价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:this.data.shop_price,
            single_price:this.data.shop_b_price
          });
        };
      }else{  //客户询价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:0,
            single_price:0
          });
        };
      }
    }else if(this.data.shop_price_type==3){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && shopnum>0 && shopnum*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':that.data.tan_ku_price,'single_price':0};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(that.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=that.data.aux_kucun_list[that.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          this.setData({
            num: shopnum,
            unit_price:kucun_price_obj.true_price,
            single_price:kucun_price_obj.single_price
          });
        }else if(this.data.tan_shop_ku==0){  //非库存
          if(this.data.is_zi_cai==0 || that.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=that.data.assign_superlist[that.data.assign_super_index];
            var nor_price_obj={'true_price':that.data.shop_price,'single_price':0};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            this.setData({
              num: shopnum,
              unit_price:nor_price_obj.true_price,
              single_price:nor_price_obj.single_price,
              shop_submit_price:assign_item.submit_price
            });
          }else if(this.data.is_zi_cai==1 && that.data.assign_super_index==-10010){ //自采
            this.setData({
              num: shopnum,
              unit_price:this.data.shop_price,
              single_price:0
            });
          };
        };
      }else{  //其他模式
        this.setData({
          num: shopnum,
          unit_price:this.data.shop_price,
          single_price:0
        });
      };
    }else if(this.data.shop_price_type==4){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){//模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && shopnum>0 && shopnum*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':that.data.tan_ku_price,'single_price':that.data.tan_ku_price};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(that.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=that.data.aux_kucun_list[that.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          this.setData({
            num: shopnum,
            unit_price:kucun_price_obj.true_price,
            single_price:kucun_price_obj.single_price
          });
        }else if(this.data.tan_shop_ku==0){ //非库存
          if(this.data.is_zi_cai==0 || that.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=that.data.assign_superlist[that.data.assign_super_index];
            var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            this.setData({
              num: shopnum,
              unit_price:nor_price_obj.true_price,
              single_price:nor_price_obj.single_price,
              shop_submit_price:assign_item.submit_price
            });
          }else if(this.data.is_zi_cai==1 && that.data.assign_super_index==-10010){  //自采
            this.setData({
              num: shopnum,
              unit_price:this.data.shop_price,
              single_price:this.data.shop_price
            });
          };
        };
      }else{  //其他模式
        this.setData({
          num: shopnum,
          unit_price:this.data.shop_price,
          single_price:this.data.shop_price
        });
      };
    }else if(this.data.shop_price_type==5){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){//模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && shopnum>0 && shopnum*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':that.data.tan_ku_price,'single_price':that.data.tan_ku_price};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(that.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=that.data.aux_kucun_list[that.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          this.setData({
            num: shopnum,
            unit_price:kucun_price_obj.true_price,
            single_price:kucun_price_obj.single_price
          });
        }else if(this.data.tan_shop_ku==0){ //非库存
          if(this.data.is_zi_cai==0 || that.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=that.data.assign_superlist[that.data.assign_super_index];
            var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            this.setData({
              num: shopnum,
              unit_price:nor_price_obj.true_price,
              single_price:nor_price_obj.single_price,
              shop_submit_price:assign_item.submit_price
            });
          }else if(this.data.is_zi_cai==1 && that.data.assign_super_index==-10010){  //自采
            this.setData({
              num: shopnum,
              unit_price:this.data.shop_sale_price,
              single_price:this.data.shop_sale_price
            });
          };
        };
      }else{  //其他模式
        this.setData({
          num: shopnum,
          unit_price:this.data.shop_sale_price,
          single_price:this.data.shop_sale_price
        });
      };
    };
    this.updateshangpin(aux_obj);
    if(this.data.isInput==true){
      util.hideBuyModal(this,"isInput");
    };   
    this.setData({
      shangpin:'',
      isaufocus:true,
      isinput_col: false
    });
    if (shopnum > 0){
      this.setData({
        ["rightDatas[" + this.data.show_index + "].is_show"]: 1
      });
    }else{
      this.data.taninfo[this.data.ischoose_index].pr_num = 0;
      for (var i in this.data.taninfo) {
        if (this.data.taninfo[i].pr_num > 0) {
          this.setData({
            ["rightDatas[" + this.data.show_index + "].is_show"]: 1
          });
          return;
        }
      }
      this.setData({
        ["rightDatas[" + this.data.show_index + "].is_show"]: 0
      });
    };
  },
  addFBTap(){
    this.setData({
      isaufocus:false,
      isfeibiao: true,
      fbNum: 0,
      fbName: "",
      firstPerson: ""
    });
  },
  // 非标品单位列表
  feibiao: function () {
    var that = this;
    app.AppHttp('api/Goods/addNoProduct', {}, function (res) {
      that.setData({
        unit_list: res.data.unit_list
      })
    })
  },
  feibiaocancel() {
    this.setData({
      isfeibiao: false,
      tszhcontent: ''
    });
  },
  /**
   * 非标品确认添加
   */
  feibiaoconfirm() {
    var that = this;
    var fbNum = that.data.fbNum;
    var fbName = that.data.fbName;
    var fbDanWei = that.data.value;
    if (fbName == '') {
      wx.showToast({
        title: '请输入名称',
      })
    } else if (fbDanWei == '') {
      wx.showToast({
        title: '请选择单位',
      })
    }else {
      wx.request({
        url: app.globalData.url + 'api/Order/addNoProductToOrder',
        method: 'POST',
        data: {
          supplier_id: that.data.supplier_id,
          order_id: that.data.order_id,
          pr_name: "**"+fbName,
          pr_unit: fbDanWei,
          num: fbNum,
          detail_remark: that.data.tszhcontent,
          one_class:0
        },
        success: function (res) {
          console.log(JSON.stringify(res.data))
          if (fbNum > 0) {
            that.setData({
              shangpin:'',
              isaufocus:true,
              isfeibiao: false
            });
            app.globalData.dingdan_show = 1;
            wx.showLoading({
              title: '添加成功',
              duration: 500
            });
          }
        },
      })
      that.setData({
        tszhcontent: ''
      })
    }
  },
  fbminTap(event) {
    var fbNum = event.currentTarget.dataset.id;
    if(fbNum<1){
      fbNum=0;
    }else{
      fbNum--; 
      if((fbNum*1)%1!=0){
        fbNum=parseFloat((fbNum*1).toFixed(2));
      };  
    };
    this.setData({
      fbNum: fbNum,
      holderFBNum: fbNum
    });    
  },
  fbnumInput(event) {
    util.showBuyModal(this);
    var index = event.currentTarget.dataset.id;;
    this.setData({
      content:'',
      isInput:true,
      tan_single_price:'',
      inputIndex: index,
      holderFBNum: this.data.fbNum,
      inputFBNum:'',
      is_move_gif:false,
      assign_superlist:[],
      tanimg:'/upload/smallprogram/noshopimg.png',
      tanname: this.data.fbName?this.data.fbName:'未填写名称',
      tanunit: this.data.firstPerson
    });
  },
  fbaddTap(event) {
    var fbNum = event.currentTarget.dataset.id;
    fbNum = parseFloat(fbNum);
    fbNum++;
    this.setData({
      fbNum: fbNum,
      holderFBNum: fbNum
    })
  },
  // 非标品名称输入
  setFBName(event) {
    var fbName = event.detail.value;
    this.setData({
      fbName: fbName
    })
  },
  // 非标品单位弹窗
  setFBDanWei: function (event) {
    var fbDanWei = this.data.firstPerson;
    var selectdanwei = this.data.selectdanwei;
    if (selectdanwei == true) {
      util.hideBuyModal(this,"selectdanwei");
    } else {
      util.showBuyModal(this);
      this.setData({
        selectdanwei: true
      })
    }
  },
  confirmfb(){
    if (this.data.inputFBNum > 9999.99) {
      wx.showToast({
        title: '数量过大',
      })
      return;
    }
    if (this.data.inputFBNum == ''){
        util.hideBuyModal(this,"isInput");
        this.setData({
          holderFBNum:""
        });
    }else{
      var fbNum = this.data.inputFBNum * 1;
      util.hideBuyModal(this,"isInput");
      this.setData({
        fbNum:fbNum,
        holderFBNum:""
      });
    } 
  },
  inputck: function (e) {
    var shangpin = e.detail.value;
    this.setData({
      shangpin: shangpin
    });
    app.globalData.task.abort();  //中断上一次搜索
    this.viewck();
  },
  viewck: function () {
    this.setData({
      rightDatas:[],
      guige: [],
      page: 1,
      load: "搜索中"
    })
    this.upshoplist();
  },
  upshoplist: function () {
    if(this.data.page==1){
      wx.showLoading({
        title: this.data.load
      });
    }; 
    var that = this;
    var v = that.data.shangpin;
    app.AppHttp('api/Order/SearchOrderGood', {
      keyword: v,
      supplier_id: that.data.supplier_id,
      order_id: that.data.order_id,
      page: that.data.page,
      limit: 20,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res));
      if (that.data.page == 1) {
        that.setData({
          loading:0,
          price_type:res.data.price_type,     //<3 非询价  ==3询价
          show_price_type:res.data.show_price_type,
          rightDatas: res.data.list,
          allpage: res.data.page_count
        });
      } else {
        that.setData({
          loading:0,
          price_type:res.data.price_type,     //<3 非询价  ==3询价
          show_price_type:res.data.show_price_type,
          rightDatas: that.data.rightDatas.concat(res.data.list),
          allpage: res.data.page_count
        });
      }
      console.log(that.data.page);
      console.log(that.data.allpage);
      wx.hideLoading();
    });
  },
  searchClick: function () {
    app.globalData.task.abort();  //中断上一次搜索
    this.viewck();
  },
  itemAddClick: function (e) {
    this.numInput(e)
  },
  numInput: function (e) {
    var index = e.currentTarget.id;
    var info = this.data.rightDatas[index];
    var is_up=e.currentTarget.dataset.is_up;   //是否下架
    var is_ku=e.currentTarget.dataset.is_ku;   //是否属于库存商品
    var is_zi_cai=this.data.is_zi_cai;   //客户是否自采
    var tan_is_auxiliary=e.currentTarget.dataset.is_auxiliary;  //是否开启辅助单位
    if(this.data.good_dir_set==1){  
      tan_is_auxiliary=0;  //供应商目录 的 相关辅助单位（等企业相关辅助单位完善后可做），目前假定没有辅助单位
    };
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){  //模式5
      if(is_up*1+is_zi_cai*1==0 && is_ku==0){
        return;
      };
      console.log(is_ku);
      if(is_ku==0){
        this.setData({
          tan_shop_ku:0,  //当前商品不属于库商品
          assign_superlist:[]
        });
        if(is_zi_cai==0){  //非自采
          this.setData({
            is_move_gif:true,
            assign_super_index:0,
            tan_is_auxiliary:tan_is_auxiliary,
            aux_kucun_index:-1,
            aux_kucun_list:[],
          });
          this.assign_super_fun(info.id,tan_is_auxiliary);
        }else if(is_zi_cai==1){  //自采
          this.setData({
            assign_super_index:-10010,
            aux_kucun_index:-1,
            aux_kucun_list:[],
            is_move_gif:false,
            tan_is_auxiliary:tan_is_auxiliary
          });
        };
      }else if(is_ku==1){
        if(this.data.is_ctrl_stock==1 && info.stock_num_show<=0){
          wx.showLoading({
            title:'库存不足',
            duration:500
          });
          return;
        };
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          assign_super_index:-1,
          aux_kucun_index:tan_is_auxiliary==1?0:-1,
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_shop_ku:1  //当前商品属于库商品
        });
        if(tan_is_auxiliary==1){
          this.aux_kucun_fun(info.id);
        };
      }; 
    }else{
      if(info.price_type<=2){ //供应-批发
        this.setData({
          aux_pf_list:[],
          tan_is_auxiliary:tan_is_auxiliary
        })
        if(tan_is_auxiliary==0){
          this.setData({
            aux_pf_index:-1,
            is_move_gif:false
          })
        }else if(tan_is_auxiliary==1){ //辅助单位
          this.setData({
            aux_pf_index:0,
            is_move_gif:true
          })
          this.aux_gy_pf_fun(info.id);
        };
      }else{
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          assign_super_index:-1,
          is_move_gif:false
        });
      }
    };   
    util.showBuyModal(this);
    let tan_true_price='';
    let tan_single_price='';
    if(this.data.price_type==2){
      tan_true_price=info.level_true_price;
      tan_single_price=info.level_single_price;
    }else{
      tan_true_price=info.true_price;
      tan_single_price=info.single_price;
    };
    this.setData({
      content:'',
      tszhcontent:'',
      isaufocus:false,
      isInput: true,
      inputIndex: index,
      inputNum: '',
      tanimg: info.cover,
      tanname: info.trade_name,
      tanalias:info.trade_alias,
      tanspecs:info.specs,
      tan_fixed_show:info.is_show_price,  //是否显示固定价
      tanunit_index: info.help_unit>0?info.help_unit:info.unit,
      tanunit: info.help_unit>0?info.help_unit_name:info.unit_name,
      tanunit_init:info.unit_name,
      tannum: info.pr_num,
      tan_price_type:info.price_type,
      tan_true_price:tan_true_price,
      tan_single_price:tan_single_price,
      tan_sale_price:info.sale_price,
      shop_is_same:info.is_same,  //是否开启了采销同价
      tan_ku_price:info.is_same==0?info.stock_sale_price:info.stock_price_show,
      tan_ku_num:info.stock_num_show,
      is_tan_ku_price:info.is_show_stock_price,
      is_tan_ku_num:info.is_show_stock_num
    }); 
  },
  // 选择菜品时的供应商和价格
  assign_super_fun:function(good_id,is_aux){
    var that = this; 
    app.AppHttp('api/Shopping/getGoodSupplierInfo', {
      order_id:that.data.order_id,
      client_id: app.globalData.loginInfo.id,
      good_id:good_id,
      is_aux:is_aux
    }, function (res) {
      var assign_superlist = res.data.filter(function (item) {
        return (that.data.supplier_id == item.supplier_id);
      });  
      console.log(assign_superlist);  
      that.setData({
        is_move_gif:false,
        assign_superlist:assign_superlist
      });   
      if(that.data.isinput_col && (assign_superlist.length==0 && that.data.is_zi_cai==1 || assign_superlist.length==1 && that.data.is_zi_cai==0)){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };   
    });   
  },
  // 库存辅助单位接口
  aux_kucun_fun(good_id){
    var that = this; 
    app.AppHttp('api/Shopping/getStockAuxUnit', {
      order_id:that.data.order_id,
      good_id:good_id,
      client_id:app.globalData.loginInfo.id
    }, function (res) {
      console.log(JSON.stringify(res.data));  
      that.setData({
        is_move_gif:false,
        aux_kucun_list:res.data
      });
      if(that.data.isinput_col && res.data.length==1){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };
    });  
  },
  // 供应-批发端辅助单位接口
  aux_gy_pf_fun(good_id){
    var that = this; 
    app.AppHttp('api/Shopping/getSupplierGoodsAux', {
      order_id:that.data.order_id,
      client_id:app.globalData.loginInfo.id,
      good_id:good_id,
      supplier_id:that.data.supplier_id
    }, function (res) {
      console.log(JSON.stringify(res.data)); 
      let aux_pf_list=[];
      if(res.data.goodsAuxiliaryList.length>0){
        aux_pf_list = res.data.goodsAuxiliaryList.filter((c,i) => {
          if(c.is_order_unit==1){
            if(that.data.price_type==2){
              res.data.goodsAuxiliaryList[i].sub_price = res.data.goodsAuxiliaryList[i].level_sub_price;
              return true;
            }else{
              res.data.goodsAuxiliaryList[i].sub_price = res.data.goodsAuxiliaryList[i].his_sub_price;
              return true;
            };
          };
        });
      };
      that.setData({
        is_move_gif:false,
        aux_pf_list:aux_pf_list
      });
      if((that.data.price_type==1 || that.data.price_type==4) && aux_pf_list.length==0){
        that.setData({
          tan_is_auxiliary:0 //**有辅助单位，但还没有历史价，按无辅助单位处理
        })
        if(that.data.isinput_col){
          that.open_tan_info();
        }
      };
      if((that.data.isinput_col || that.data.is_open_type=='normal') && aux_pf_list.length==1){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };
    });  
  },
  setValue: function () {
    this.setData({
      inputNum: this.data.content
    });
  },
  fbsetValue: function () {
    this.setData({
      inputFBNum: this.data.content
    });
  },
  confirm(){
    if (this.data.inputNum == 0){
      if(this.data.inputNum != '' || this.data.tannum==0){
        wx.showLoading({
          title:'输入不能为0',
          duration:500
        });
        return;
      }else{
        this.setData({
          inputNum:this.data.tannum
        });
      }  
    };
    var info = this.data.rightDatas[this.data.inputIndex];
    if (info.pr_num>0){
      info.detail_id=1;
    }
    if (this.data.inputNum>9999.99){
      wx.showToast({
        title: '数量过大',
      })
      return;
    };
    info.pr_num = this.data.inputNum == '' ? 0 : this.data.inputNum * 1;
    var aux_obj={'is_aux':0,'pr_unit':this.data.tanunit_index,'sub_unit_num':1};
    if(info.price_type==1 || info.price_type==2){
      if(this.data.price_type==2){ //客户级别价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:info.level_true_price,
            single_price:info.level_single_price
          });
        }
      }else if(this.data.price_type==1){ //客户历史价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:0,
            single_price:0
          });
        }
      }else if(this.data.price_type==4){ //客户固定价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:info.true_price,
            single_price:info.single_price
          });   
        };
      }else{   //客户询价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:0,
            single_price:0
          });
        };
      }
    }else if(info.price_type==3){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && info.pr_num>0 && info.pr_num*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':this.data.tan_ku_price,'single_price':0};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(this.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=this.data.aux_kucun_list[this.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          this.setData({
            unit_price:kucun_price_obj.true_price,
            single_price:kucun_price_obj.single_price
          });
        }else if(this.data.tan_shop_ku==0){ //非库存商品
          if(this.data.is_zi_cai==0 || this.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=this.data.assign_superlist[this.data.assign_super_index];
            var nor_price_obj={'true_price':info.true_price,'single_price':0};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            this.setData({
              unit_price:nor_price_obj.true_price,
              single_price:nor_price_obj.single_price,
              shop_submit_price:assign_item.submit_price
            });
          }else if(this.data.is_zi_cai==1 && this.data.assign_super_index==-10010){ //自采
            this.setData({
              unit_price:info.true_price,
              single_price:0
            });
          };     
        };
      }else{   //其他模式
        this.setData({
          unit_price:info.true_price,
          single_price:0
        });
      }
    }else if(info.price_type==4){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && info.pr_num>0 && info.pr_num*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':this.data.tan_ku_price,'single_price':this.data.tan_ku_price};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(this.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=this.data.aux_kucun_list[this.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          this.setData({
            unit_price:kucun_price_obj.true_price,
            single_price:kucun_price_obj.single_price
          });
        }else if(this.data.tan_shop_ku==0){ //非库存商品
          if(this.data.is_zi_cai==0 || this.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=this.data.assign_superlist[this.data.assign_super_index];
            var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            this.setData({
              unit_price:nor_price_obj.true_price,
              single_price:nor_price_obj.single_price,
              shop_submit_price:assign_item.submit_price
            });
          }else if(this.data.is_zi_cai==1 && this.data.assign_super_index==-10010){ //自采
            this.setData({
              unit_price:info.true_price,
              single_price:info.true_price
            });
          };
        };
      }else{  //其他模式
        this.setData({
          unit_price:info.true_price,
          single_price:info.true_price
        });
      }
    }else if(info.price_type==5){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.tan_shop_ku==1){ //库存商品
          if(this.data.is_ctrl_stock==1 && info.pr_num>0 && info.pr_num*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':this.data.tan_ku_price,'single_price':this.data.tan_ku_price};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(this.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=this.data.aux_kucun_list[this.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          this.setData({
            unit_price:kucun_price_obj.true_price,
            single_price:kucun_price_obj.single_price
          });
        }else if(this.data.tan_shop_ku==0){ //非库存商品
          if(this.data.is_zi_cai==0 || this.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=this.data.assign_superlist[this.data.assign_super_index];
            var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            this.setData({
              unit_price:nor_price_obj.true_price,
              single_price:nor_price_obj.single_price,
              shop_submit_price:assign_item.submit_price
            });
          }else if(this.data.is_zi_cai==1 && this.data.assign_super_index==-10010){  //自采
            this.setData({
              unit_price:info.sale_price,
              single_price:info.sale_price
            });
          };
        };
      }else{  //其他模式
        this.setData({
          unit_price:info.sale_price,
          single_price:info.sale_price
        });
      };
    };
    util.hideBuyModal(this,"isInput");
    wx.showLoading({
      title: '添加成功',
      duration: 500
    });
    this.setData({
      shangpin:'',
      isaufocus:true,
      ["rightDatas[" + this.data.inputIndex + "]"]: info,
      inputNum: "",
      shopid: info.id,
      detail_id: info.detail_id,
      num: info.pr_num,
      content:"",
      inputIndex: -1
    });
    this.updateshangpin(aux_obj);
  },
  cancel: function () {
    util.hideBuyModal(this,"isInput");
    this.setData({
      inputIndex: -1,
      inputNum: "",
      tannum:'',
      shopnum:'',
      shoppr_num:'',
      tszhcontent: '',
      tangui: "",
      content: ""
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      is_zi_cai:options.is_zi_cai,  //是否自采
      is_open_standard:options.is_open_standard, //是否开启非标
      good_dir_set:app.globalData.loginInfo.good_dir_set,    //目录类型
      client_is_store:app.globalData.loginInfo.client_is_store,  //<0未加入集团，==0加入集团无库 ,==1加入集团有库
      purchase_type:app.globalData.loginInfo.purchase_type,  //1门店采购，2总部采购
      is_ctrl_stock:app.globalData.loginInfo.is_ctrl_stock,  //库存控制开启（1开，0关），1库存不足则不可下单
      group_id:app.globalData.loginInfo.group_id,
      group_name:app.globalData.loginInfo.group_name,  //集团名称
      type_level:app.globalData.type_level,
      order_id: options.order_id,
      supplier_id: options.supplier_id,
      supplier_name:options.supplier_name,
      order_status: options.order_status,
      unit_height:0.64* wx.getSystemInfoSync().windowHeight,
      two_specs_h:0.6* wx.getSystemInfoSync().windowHeight,
      raplace_h:0.75*wx.getSystemInfoSync().windowHeight
    });
    if(options.supplier_id>0){ //获取一级分类(super) 
      if(app.globalData.loginInfo.good_dir_set==1 || app.globalData.loginInfo.good_dir_set<1 && app.globalData.type_level==0){   //获取一级分类(super)
        app.AppHttp('api/Client/supperDetail', {  
          openid: app.globalData.openid,
          id: options.supplier_id
        }, function (res) {
          that.setData({
            is_non_standard: res.data.data.is_non_standard
          });
        })
      };
      app.AppHttp('api/Orders/getOneClass', {
        openid: app.globalData.openid,
        supplier_id: options.supplier_id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        that.setData({
          types:res.data
        })
      });
      wx.request({
        url: app.globalData.url + 'supplier_coop_custom/'+options.supplier_id+'/'+app.globalData.loginInfo.id,
        method: "GET",
        success: function(res) {
          console.log(res.data.data.price_type);
          that.setData({
            price_type:res.data.data.price_type
          });
        }
      });    
    }else{    //获取一级分类(client)
      app.AppHttp('api/Order/getClientOneClass', {
        client_id:app.globalData.loginInfo.id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        that.setData({
          types:res.data
        })
      });
    };
  },
  noneEnoughPeople:function(){
    return;
  },
  // 添加到订单
  updateshangpin:function(aux_obj){
    var that = this;
    var num=that.data.num;
    if (/^0\d*$/.test(num) && num>0) {   //当输入字符为整数且0开头
      num = num.replace(/^0+/, "")   //去掉前面的0
    };
    app.AppHttp('api/Order/AddGoodsToOrderfb', {
      id: that.data.shopid,
      order_id: that.data.order_id,
      detail_id: that.data.detail_id,
      num: num,
      order_status:that.data.order_status,
      detail_remark: that.data.tszhcontent,
      unit_price:that.data.unit_price,
      single_price:that.data.single_price,
      is_aux:aux_obj.is_aux,
      pr_unit:aux_obj.pr_unit,
      sub_unit_num:aux_obj.sub_unit_num,
      submit_price:that.data.shop_submit_price
    }, function (res) {
      console.log(JSON.stringify(res.data));
      app.globalData.dingdan_show = 1;
      that.setData({
        isinput_col: false,   //关闭收藏弹窗
        shangpin:'',
        inputNum: "",
        content:"",
        tszhcontent: '',
        inputIndex: -1
      });
      app.UpAsso('java_order/update_order_allow_batch/'+that.data.order_id, //订单关联
        function (ress) {   
      });
    })
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,3);
    if(this.data.isfeibiao==true){
      this.fbsetValue(); 
    }else if(this.data.isinput_col==true){
      this.col_setValue();
    }else{ 
      this.setValue();
    };
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.viewck();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.page < that.data.allpage) {
      that.setData({
        page: that.data.page + 1,
        isChooseAll: false,
        load: '加载更多',
        loading:1
      });
      that.upshoplist();
    } else {
      wx.showToast({
        title: '没有了'
      });
      this.setData({
        loading:0
      });
    }
  }
})